﻿<template>
    <full-screen-dialog
        :title="titleMap[mode]"
        v-model="visible"
        :width="1000"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="门店名称" prop="storeName">
                        <el-input v-model="entityObj.storeName" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="门店全称" prop="storeFullName">
                        <el-input v-model="entityObj.storeFullName" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="品牌" prop="brandCode">
                        <el-select
                            placeholder="---请选择品牌---"
                            v-model="entityObj.brandCode"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in optionsBrand"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="地区" prop="areaCode">
                        <el-tree-select
                            filterable
                            collapse-tags
                            collapse-tags-tooltip
                            v-model="entityObj.areaCode"
                            :data="areaTree"
                            :props="{ label: 'label', value:'value',children: 'children' }"
                            :render-after-expand="false"
                            style="width: 100%"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="类型" prop="storeType">
                        <el-select
                            placeholder="---请选择门店类型---"
                            v-model="entityObj.storeType"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in optionsStoreType"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :span="12">
                    <el-form-item label-width="120px" label="联系人" prop="realName">
                        <el-input v-model="entityObj.realName" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="联系电话" prop="mobile">
                        <el-input v-model="entityObj.mobile" />
                    </el-form-item>
                </el-col>

                <el-col :span="12">
                    <el-form-item label-width="120px" label="联系地址" prop="address">
                        <el-input v-model="entityObj.address" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="结款方式" prop="paymentType">
                        <el-select
                            placeholder="---请选择结款方式---"
                            v-model="entityObj.paymentType"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in optionsPaymentType"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="开票类型" prop="invoicingType">
                        <el-select
                            placeholder="---请选择开票类型---"
                            v-model="entityObj.invoicingType"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in optionsInvoicingType"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="结款期限设置" prop="creditDay">
                        <el-input-number
                            v-model="entityObj.creditDay"
                            style="width: calc(100% - 40px);"
                        ></el-input-number>
                        <span style="line-height: 36px; margin-left: 8px;">天</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="欠款上限设置" prop="creditLimit">
                        <el-input-number
                            v-model="entityObj.creditLimit"
                            style="width: calc(100% - 40px);"
                        ></el-input-number>
                        <span style="line-height: 36px; margin-left: 8px;">万</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="电子账户">
                        <el-input v-model="entityObj.electronicAccount" disabled />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="点单机构编码">
                        <el-input v-model="entityObj.posStoreCode" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="点单出库仓库" v-if="entityObj.storeId>0">
                        <el-select
                            placeholder="---请选择点单出库仓库---"
                            v-model="entityObj.posStoreWarehouseId"
                            style="width: 100%"
                        >
                            <el-option label="---请选择---" value />
                            <el-option
                                v-for="item in optionsWarehouses"
                                :key="item.warehouseId"
                                :label="item.warehouseName"
                                :value="item.warehouseId"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="状态">
                        <el-switch
                            v-model="entityObj.status"
                            :active-value="1"
                            :inactive-value="0"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="备注">
                        <el-input v-model="entityObj.remarks" type="textarea" :rows="2" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                v-if="mode != 'show'"
                type="primary"
                :loading="isSaveing"
                @click="submit()"
            >保 存</el-button>
        </template>

        <!-- 门店 -->
        <template>
            <el-dialog
                v-model="storeState"
                title="存在相应的门店，是否与要添加的匹配。"
                :center="true"
                :width="1000"
                append-to-body
            >
                <el-row style="margin-bottom: 10px;">
                    <match-store-dialog ref="scMatchStoreChoice" :data="entityObj.stores"></match-store-dialog>
                </el-row>
                <template #footer>
                    <el-button @click="storeState = false">取 消</el-button>
                    <el-button type="primary" :loading="isSaveing" @click="saveStore">否</el-button>
                    <el-button type="primary" :loading="isSaveing" @click="setStoreChoice">是</el-button>
                </template>
            </el-dialog>
        </template>
    </full-screen-dialog>
</template>

<script>
import tool from "@/utils/tool";
import matchStoreDialog from "./matchStore";
export default {
    components: {
        matchStoreDialog,
    },
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            entityObj: {
                status: 1,
            },
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            storeState: false,
            //验证规则
            rules: {
                storeName: [
                    {
                        required: true,
                        type: "string",
                        message: "请填写门店名称",
                    },
                ],
                storeFullName: [
                    {
                        required: true,
                        type: "string",
                        message: "请填写门店全称",
                    },
                ],
                brandCode: [
                    {
                        required: true,
                        type: "string",
                        message: "请填写门店品牌",
                    },
                ],
                brandName: [
                    {
                        required: true,
                        type: "string",
                        message: "请填写品牌名称",
                    },
                ],
                areaCode: [
                    { required: true, type: "string", message: "请填写" },
                ],
                realName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                mobile: [{ required: true, type: "string", message: "请填写" }],
                address: [
                    { required: true, type: "string", message: "请填写" },
                ],
                paymentType: [
                    { required: true, type: "number", message: "请填写" },
                ],
                invoicingType: [
                    { required: true, type: "number", message: "请填写" },
                ],
                creditDay: [
                    {
                        required: true,
                        type: "number",
                        min: 0,
                        message: "请填写",
                    },
                ],
                creditLimit: [
                    {
                        required: true,
                        type: "number",
                        min: 0,
                        message: "请填写",
                    },
                ],
                storeType: [
                    { required: true, type: "number", message: "请填写" },
                ],
            },
            optionsWarehouses: [],
            optionsStoreType: [],
            optionsPaymentType: [],
            optionsInvoicingType: [],
            areaTree: [],
            purchaseDept: [],
            optionsBrand: [],
        };
    },
    methods: {
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    this.entityObj.brandName = this.optionsBrand.find(
                        (p) => p.value == this.entityObj.brandCode
                    )?.label;

                    if (this.mode == "Add") {
                        var verifyValue =
                            await this.$API.auth.PostProcessApi.post(
                                "store/pre-verify",
                                { ...this.entityObj }
                            );
                        if (verifyValue.status == 200) {
                            var list = verifyValue.response;
                            this.entityObj.preGuid = verifyValue.msg;
                            this.entityObj.stores = list;

                            if (list && list?.length > 0) {
                                this.storeState = true;
                                this.isSaveing = false;
                                return;
                            }
                        }
                    }
                    await this.saveStore();
                }
            });
        },
        //保存门店
        async saveStore() {
            this.storeState = false;
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    this.mode == "Add" ? "store/create" : "store/update",
                    { ...this.entityObj }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$emit("fatherMethod");
                    this.isSaveing = false;
                    this.visible = false;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                    this.isSaveing = false;
                }
            } catch (error) {
                this.isSaveing = false;
                return false;
            }
        },
        //选择门店进行绑定
        async setStoreChoice() {
            let stores = this.$refs.scMatchStoreChoice.selectionRows;
            if (!stores || stores.length == 0) {
                this.$message.error("请选择一个匹配门店");
                return;
            }

            if (!stores || stores.length > 1) {
                this.$message.error("请选择一个匹配门店");
                return;
            }
            this.isSaveing = true;
            this.entityObj.storeIds = stores.map((p) => p.storeId);
            //绑定
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "purchase-unity-store-relation/set-relations",
                    { ...this.entityObj }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$emit("fatherMethod");
                    this.isSaveing = false;
                    this.visible = false;
                } else {
                    this.isSaveing = false;
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.isSaveing = false;
                this.loading = false;
                this.storeState = false;
                return false;
            }
        },
        //表单注入数据
        async setData(data) {
            this.entityObj = {
                ...data,
                creditDay: Number(data.creditDay),
                areaCode: data.cityCode,
            };

            //获取店铺仓库
            if (this.entityObj.storeId > 0) {
                try {
                    var returnValue = await this.$API.auth.GetProcessApi.get(
                        `store/warehouses/${this.entityObj.storeId}`,
                        { }
                    );
                    if (returnValue.status == 200) {
                        this.optionsWarehouses = returnValue.response;
                    } else {
                        this.$message.error("操作失败:" + returnValue.msg);
                    }
                } catch (error) {
                    this.loading = false;
                    return false;
                }
            }
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["StoreType", "PaymentType", "InvoicingType"] }
                );
                if (returnValue.status == 200) {
                    this.optionsStoreType = returnValue.response.find(
                        (p) => p.code == "StoreType"
                    ).optionsDetails;
                    this.optionsPaymentType = returnValue.response.find(
                        (p) => p.code == "PaymentType"
                    ).optionsDetails;
                    this.optionsInvoicingType = returnValue.response.find(
                        (p) => p.code == "InvoicingType"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/dic-options",
                    { ParentBaseDictionaryCodes: ["001"] }
                );
                if (returnValue.status == 200) {
                    this.optionsBrand = returnValue.response.find(
                        (p) => p.code == "001"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "system-area/tree",
                    {}
                );
                if (returnValue.status == 200) {
                    this.areaTree = returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "purchase-dept/tree",
                    {}
                );
                if (returnValue.status == 200) {
                    this.purchaseDept = returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        await this.initOptions();
    },
};
</script>

<style></style>
